package com.go.where.manager.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

	public static final String C_DATE_PATTON_DEFAULT = "yyyy-MM-dd";

	public static void main(String[] args) {
		Date d = longToDate(1448350996956L);
		System.out.println(format(d, "yyyy-MM-dd HH:mm:ss"));
	}

	public static Date longToDate(long millis) {
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(millis);
		return c.getTime();
	}

	public static String getTodayBegin() {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		Date today = c.getTime();
		return format(today, "yyyy-MM-dd HH:mm:ss");
	}

	public static String getTodayEnd() {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.HOUR_OF_DAY, 23);
		c.set(Calendar.MINUTE, 59);
		c.set(Calendar.SECOND, 59);
		Date today = c.getTime();
		return format(today, "yyyy-MM-dd HH:mm:ss");
	}

	public static String getToday() {
		Calendar c = Calendar.getInstance();
		Date today = c.getTime();
		return format(today, "yyyy-MM-dd");
	}

	/**
	 * 将Timestamp类型的日期转换为系统参数定义的格式的字符串。
	 * 
	 * @param aTs_Datetime
	 *            需要转换的日期。
	 * @return 转换后符合给定格式的日期字符串
	 */
	public static String format(Date aTs_Datetime) {
		return format(aTs_Datetime, C_DATE_PATTON_DEFAULT);
	}

	public static String format(Date aTs_Datetime, String as_Format) {
		if (aTs_Datetime == null || as_Format == null)
			return null;

		SimpleDateFormat dateFromat = new SimpleDateFormat();
		dateFromat.applyPattern(as_Format);

		return dateFromat.format(aTs_Datetime);
	}

	public static Date addDays(int days) {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DAY_OF_MONTH, days);
		return cal.getTime();
	}

	/**
	 * 获取昨天的日期<br>
	 * yesterday <br>
	 * 格式：yyyy-MM-dd
	 */
	public static String getbeforeYesterday() {
		String now = null;
		Date currentTime = new Date();
		long date = currentTime.getTime();
		long beYesterday = date - 1 * 24 * 3600 * 1000;
		Date beDate = new Date(beYesterday);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		now = format.format(beDate);
		return now;
	}

	/**
	 * 获取今天的日期 <br>
	 * 格式：yyyy-MM-dd
	 * 
	 * @return
	 */
	public static String getDateOfToday() {
		String now = null;
		Date currentTime = new Date();
		long date = currentTime.getTime();
		Date beDate = new Date(date);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		now = format.format(beDate);
		return now;
	}

	public static Date parse(String pattern, String dateStr) {
		try {
			SimpleDateFormat format = new SimpleDateFormat(pattern);
			return format.parse(dateStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}
